package com.neusoft.SPNursingHome.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.SPNursingHome.dao.FoodDao;
import com.neusoft.SPNursingHome.po.Admin;
import com.neusoft.SPNursingHome.po.Food;
import com.neusoft.SPNursingHome.po.ResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FoodServiceImpl implements FoodService {

    @Autowired
    private FoodDao dao;

    @Override
    public IPage<Food> listChange(String foodType, String foodName, long page, long size) {
        // 构造分页对象
        Page<Food> pager = new Page<>(page, size);

        // 构造查询条件
        QueryWrapper<Food> qw = new QueryWrapper<>();
        if (foodType != null && !foodType.isBlank()) {
            qw.like("food_type", foodType.trim());
        }
        if (foodName != null && !foodName.isBlank()) {
            qw.like("food_name", foodName.trim());
        }

        // 执行分页查询
        return dao.selectPage(pager, qw);
    }
    @Override
    public ResponseBean<List<Food>> listAll(String foodType) {
        QueryWrapper<Food> qw = new QueryWrapper<>();
        if (foodType != null && !foodType.isBlank()) {
            // 按 ad_name 模糊匹配
            qw.like("food_type", foodType.trim());
        }
        List<Food> list = dao.selectList(qw);
        return new ResponseBean<>(list);
    }

    @Override
    public ResponseBean<Food> getById(Integer id) {
        Food f = dao.selectById(id);
        if (f == null) {
            return new ResponseBean<>(404, "未找到对应 Food 记录");
        }
        return new ResponseBean<>(f);
    }

    @Override
    public ResponseBean<Void> create(Food food) {
        int rows = dao.insert(food);
        if (rows != 1) {
            return new ResponseBean<>(500, "Food 新增失败");
        }
        return new ResponseBean<>();
    }

    @Override
    public ResponseBean<Void> update(Food food) {
        int rows = dao.updateById(food);
        if (rows != 1) {
            return new ResponseBean<>(500, "Food 更新失败");
        }
        return new ResponseBean<>();
    }

    @Override
    public ResponseBean<Void> deleteById(Integer id) {
        int rows = dao.deleteById(id);
        if (rows != 1) {
            return new ResponseBean<>(500, "Food 删除失败");
        }
        return new ResponseBean<>();
    }
}
